/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.lang.StringEscapeUtils;

import v4view.core.utils.ResourceManager;

/**
 * Represents plan text.
 * @author J Patrick Davenport
 *
 */
public class Content extends PageElement implements IBlockElement, IInlineElement {
	private String			content;
	private ResourceManager	resourceManager;
	private String			resourceBundle;
	private String			resourceKey;
	private boolean			escapeHtml;

	{
		this.setTag("");
		this.setEscapeHtml(true);
	}

	public Content(final String _content) {
		this(_content, true);
	}

	/**
	 * 
	 * @param _content the raw string you want to shove into your DOM.
	 * @param _shouldEscapeHtml specifies is the content should escape greater than, less than signs, etc.
	 * By default it does.
	 */
	public Content(final String _content, final boolean _shouldEscapeHtml) {
		this.content = _content;
		this.escapeHtml = _shouldEscapeHtml;
	}

	public Content(final String _resourceBundle, final String _key) {
		this.resourceBundle = _resourceBundle;
		this.resourceKey = _key;
		this.resourceManager = ResourceManager.getManager();
		this.addLoadCallBack(new IPageElementCallback<Content>() {

			@Override
			public void execute(final Content _elementBeingLoaded) {
				Content.this.setContentByLocal();
			}
		});
	}

	private void setContentByLocal() {
		final String value = this.resourceManager.getValue(this.resourceBundle, this.resourceKey, this.getRequest().getLocale());
		this.setContent(value);
	}

	@Override
	public void render() throws IOException {
		final PrintWriter writer = this.getWriter();
		String prettyPrint = "";

		if (this.isPrettyPrint()) {
			final int numIn = this.getTabsIn();
			final StringBuilder sb = new StringBuilder();
			for (int i = 0; i < numIn; i++) {
				sb.append("\t");
			}
			prettyPrint = sb.toString();
		}

		final StringBuilder sb = new StringBuilder();
		final String myContent = this.content == null ? "" : this.content;
		sb.append(prettyPrint);
		sb.append(myContent);
		final String html = this.escapeHtml ? StringEscapeUtils.escapeHtml(sb.toString()) : sb.toString();
		if (this.isPrettyPrint()) {
			writer.println(html);
		} else {
			writer.print(html);
		}
	}

	@Override
	public Content setElementId(final String _elementId) {
		return (Content) super.setElementId(_elementId);
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(final String _content) {
		this.content = _content;
	}

	@Override
	public String toString() {
		return this.content;
	}

	public boolean isEscapeHtml() {
		return this.escapeHtml;
	}

	public void setEscapeHtml(final boolean _escapeHtml) {
		this.escapeHtml = _escapeHtml;
	}
}
